import PARTS_SETS from './part-sets';
import * as utils from '../../../../utils/helps';

//纵向图表通用配置
const BASIC_VERTICAL_CONFIG = [
  'basic',
  'title',
  'legend',
  'grid',
  'verticalLabel',
  'tip',
  'toolbox',
];

//横向图表通用配置
const BASIC_HORIZENTAL_CONFIG = [
  'basic',
  'title',
  'legend',
  'grid',
  'horizentalLabel',
  'tip',
  'toolbox',
];

//图表默认配置映射集
const CONFIG_SETS = {
  simpleLine: [...BASIC_VERTICAL_CONFIG, 'xAxis', 'yAxis', 'line'], //简单线图表
  doubleLine: [...BASIC_VERTICAL_CONFIG, 'xAxis', 'doubleYAxis', 'line'], //双轴线图表
  stackLine: [...BASIC_VERTICAL_CONFIG, 'xAxis', 'yAxis', 'stackChart', 'line'], //堆栈线图表
  simpleHistogram: [...BASIC_VERTICAL_CONFIG, 'xAxis', 'yAxis', 'bar'], //简单柱图表
  doubleHistogram: [...BASIC_VERTICAL_CONFIG, 'xAxis', 'doubleYAxis', 'bar'], //双轴柱图表
  stackHistogram: [...BASIC_VERTICAL_CONFIG, 'xAxis', 'yAxis', 'stackChart', 'bar'], //堆栈柱图表
  lineHistogram: [...BASIC_VERTICAL_CONFIG, 'xAxis', 'yAxis', 'mixinChart', 'bar'], //混合线柱图表
  simpleBar: [...BASIC_HORIZENTAL_CONFIG, 'xAxis', 'yAxis', 'bar'], //简单条形图表
  doubleBar: [...BASIC_HORIZENTAL_CONFIG, 'doubleXAxis', 'yAxis', 'bar'], //双轴条形图表
  stackBar: [...BASIC_HORIZENTAL_CONFIG, 'xAxis', 'yAxis', 'stackChart', 'bar'], //堆栈条形图表
  pie: [...BASIC_HORIZENTAL_CONFIG, 'pie'], //饼图
  scatter: [...BASIC_HORIZENTAL_CONFIG, 'xAxis', 'yAxis', 'scatter'], //散点图
  gauge: ['basic', 'title', 'grid', 'tip', 'toolbox', 'gauge'], //仪表盘
  table: ['tableBasic', 'tableTitle', 'tableColumn', 'tableRow'], //表格
};

//图表配置工厂
const ConfigFactory = {
  parts: PARTS_SETS,
  sets: CONFIG_SETS,
  /**
   * 获取图表对应设置部件集合
   * @param {string} chartType 图表类型
   */
  getParts(chartType) {
    let chartSets = this.sets[chartType] || [];
    return this.parts.filter(p => chartSets.indexOf(p.partId) >= 0);
  },
  /**
   * 获取图表默认配置选项
   * @param {string} chartType 图表类型
   * @param {Array} metrics 数值
   * @param {Array} dimension 维度
   */
  getChartConfig(chartType, metrics, dimension) {
    let chartParts = this.getParts(chartType);
    let chartSettings = { metrics, dimension };
    let chartExtend = {};
    let chartOptions = {};

    //合并配置属性
    chartParts.forEach(p => Object.assign(chartSettings, p.defaultOpts.settings));
    chartParts.forEach(p => Object.assign(chartExtend, p.defaultOpts.extend));
    chartParts.forEach(p => Object.assign(chartOptions, p.defaultOpts));
    chartOptions.settings = chartSettings;
    chartOptions.extend = chartExtend;

    //动态生成配置属性
    this._parseSeries(chartOptions);
    this._parseAxis(chartOptions);
    this._parseOthers(chartOptions);

    return utils.deepCopy(chartOptions);
  },
  /**
   * 获取表格配置
   */
  getTableConfig() {
    let tableParts = this.getParts('table');
    let tableSettings = {
      excludeFields: [],
    };
    tableParts.forEach(p => Object.assign(tableSettings, p.defaultOpts.settings));
    return tableSettings;
  },
  /**
   * 按维度动态生成Series默认配置
   * @param {Object} chartOptions 图表设置
   */
  _parseSeries(chartOptions) {
    let options = {};
    Object.keys(chartOptions.settings).forEach(p => {
      if (p.indexOf('series_') >= 0) {
        options[p.substr(7)] = chartOptions.settings[p];
        delete chartOptions.settings[p];
      }
    });
    let series = [];
    chartOptions.settings.metrics.forEach(p => series.push(options));
    chartOptions.extend.series = series;
  },
  /**
   * 按维度动态生成轴默认配置
   * @param {Object} chartOptions 图表设置
   */
  _parseAxis(chartOptions) {
    let metrics = chartOptions.settings.metrics;
    let length = chartOptions.settings.metrics.length;
    //多Y轴情况
    if (chartOptions.settings.yAxisType) {
      chartOptions.settings.axisSite.left = metrics.slice(0, length - 1);
      chartOptions.settings.axisSite.right = metrics.slice(length - 1);
      chartOptions.settings.yAxisName[0] = chartOptions.settings.axisSite.left[0];
      chartOptions.settings.yAxisName[1] = chartOptions.settings.axisSite.right[0];
    }
    //多X轴情况
    if (chartOptions.settings.xAxisType) {
      chartOptions.settings.axisSite.top = metrics.slice(0, length - 1);
      chartOptions.settings.axisSite.bottom = metrics.slice(length - 1);
      chartOptions.settings.xAxisName[0] = chartOptions.settings.axisSite.top[0];
      chartOptions.settings.xAxisName[1] = chartOptions.settings.axisSite.bottom[0];
    }
  },
  /**
   * 按维度动态生成图表其他配置
   * @param {Object} chartOptions 图表设置
   */
  _parseOthers(chartOptions) {
    let metrics = chartOptions.settings.metrics;
    //图表堆叠
    if (chartOptions.settings.stack) {
      chartOptions.settings.stack.stackMetrics = metrics;
    }
    //图表混合
    if (chartOptions.settings.showLine) {
      chartOptions.settings.showLine = metrics.slice(metrics.length - 1);
    }
  },
  /**
   * 维度改变时重新设置配置
   */
  updateOptions(chartOptions) {
    this._syncSeries(chartOptions);
    this._parseAxis(chartOptions);
    this._parseOthers(chartOptions);
    return chartOptions;
  },
  /**
   * 维度改变时同步Series设置
   * @param {Object} chartOptions 图表设置
   */
  _syncSeries(chartOptions) {
    let series = chartOptions.extend.series;
    for (var i = 1; i < series.length; i++) {
      series[i] = series[0];
    }
  },
};

export default ConfigFactory;
